/******************************************************************************
 * Filename : uc_func.h
 * Copyright:
 * Created  : 2011-1-6 by franco.yin
 * Description -
 *
 ******************************************************************************/

#ifndef	__UC_FUNC_H__
#define	__UC_FUNC_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "uc_public.h"
#include "uc_list.h"
#include "uc_time.h"
#include "uc_file.h"
#include "uc_md5.h"
#include "uc_thread.h"

enum uc_user_cmd{
	USER_ADD,
	USER_MOD,
	USER_DEL,

	USER_CMD_MAX
};

enum uc_user_ret{
	USER_OK,
	USER_NOT_EXIST,
	USER_EXIST,
	USER_PASS_ERR,
	USER_INTERNAL
};

typedef struct uc_cpu_usage {
	int inited;
	UC_U32 cpu_user;
	UC_U32 cpu_sys;
	UC_U32 cpu_nice;
	UC_U32 cpu_idle;
}uc_cpu_usage_s;

typedef void (*uc_sig_fun)(int signo, char *errbuf);

extern char *uc_user_err[];

static inline int
uc_if_bigendian()
{
	union w {
		int a;
		char b;
	} c;

	c.a = 1;
	return (c.b <= 0);
}

#define uc_swap64(x) \
 ((uint64_t)( \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
  (uint64_t)(((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) ))

#define htonll(ll) uc_if_bigendian() ? ll : uc_swap64(ll)
#define ntohll(ll) uc_if_bigendian() ? ll : uc_swap64(ll)

extern void uc_sleep(int ms);
extern void uc_sleep_us(int ms, int us);
extern int uc_system(char *pFmt, ...);
extern int uc_system_ret(char *result, int result_len, char *pFmt, ...);
extern int uc_system_fork(char *pFmt, ...);
extern int uc_check_user(char *file_path, char *user_name, char *user_pass);
extern int uc_cfg_user(char *file_path, int cmd, char *user_name, char *user_pass);
extern int uc_get_cpu_useage();
extern int uc_get_mem_useage();
extern int uc_get_mem_useage_abs();
extern void uc_get_meminfo(int *mem_total, int *mem_used);
extern void uc_get_diskinfo(int *disk_total, int *disk_used);
extern int uc_get_disk_useage();
extern int uc_signal_init(int s, void (*handler) (int));
extern int uc_signal_init_ext(int sig, void (*func) (int sig, siginfo_t * si, void *p));
extern int uc_set_flag(UC_U8 *src, int bit, int flag);
extern int ucfile_get_flag(UC_U8 *src, int bit);
extern int uc_printf_bin(char *title, char *data, int len);
extern int uc_set_coredump(char *path);
#define uc_code_convert_gb2utf8(inbuf, inlen, outbuf, outlen) uc_code_convert("GB2312", "UTF-8", inbuf, inlen, outbuf, outlen)
#define uc_code_convert_utf82gb(inbuf, inlen, outbuf, outlen) uc_code_convert("UTF-8", "GB2312", inbuf, inlen, outbuf, outlen)
extern int uc_code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen);
extern int uc_random(int min, int max);
extern int uc_signal_init_p(int signo, uc_sig_fun pFun);
extern int uc_left_newest_corefile(char *path);
extern int uc_if_param(int argc, char *argv[], char *pParam);
extern int uc_write_log_file(char *file_name, char *fmt, ...);
extern int uc_udp_send(char *ip, int port, char *pBuf, int len);
extern int uc_udp_send_large(char *ip, int port, char *pBuf, int len, int max_per_send, int interval);
extern int uc_tcp_send_msg(char *ip, int port, int connect_time_out, int msg_type, char *pBuf, int len);
extern int uc_tcp_send(char *ip, int port, int connect_time_out, char *pBuf, int len);
extern int uc_unix_tcp_send(char *path, char *pBuf, int len);
extern char *uc_get_link_status_name(int link_status);
#define uc_fork(...) uc_fork_cmd(__VA_ARGS__, NULL)
//very important: uc_fork_cmd end by NULL
extern int uc_fork_cmd(const char *cmd_bin, ...);
extern int uc_pid_exist(int pid);
extern int uc_ts_buf_format(int ts_pk_size, char *ts_buf, int buf_len, int *start_len);
extern void uc_audio_display_s16(UC_U8 *pAudioBuf, int len, int *left_vol, int *right_vol);

#ifdef __cplusplus
}
#endif

#endif

